package com.example.android.compose.demo

import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.android.compose.demo.ui.theme.ComposeDemoTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val messages = listOf(
            Message("Jetpack Compose", "我们更新了"),
            Message("Jetpack Compose 2", "我们更新了,我们更新了,我们更新了,我们更新了"),
            Message("Jetpack Compose 3", "我们更新了,我们更新了,我们更新了,我们更新了"),
            Message("Jetpack Compose 4", "我们更新了,我们更新了,我们更新了,我们更新了"),
            Message("Jetpack Compose 5", "我们更新了,我们更新了,我们更新了,我们更新了"),
            Message("Jetpack Compose 6", "我们更新了,我们更新了,我们更新了,我们更新了")
        )
        enableEdgeToEdge()
        setContent {
            ComposeDemoTheme(darkTheme = false) {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Conversation(messages)
                }
            }
        }
    }
}

data class Message(val author: String, val body: String)


@Composable
fun MessageCard(msg: Message, modifier: Modifier) {
    var isExpand by remember {
        mutableStateOf(false)
    }
    val surfaceColor by animateColorAsState(targetValue = if (isExpand) Color(0xFFcccccc) else MaterialTheme.colorScheme.surface,
        label = "color change"
    )
    Surface(
        shape = MaterialTheme.shapes.medium,
        shadowElevation = 5.dp,
        modifier = Modifier
            .padding(all = 8.dp)
            .clickable {
                isExpand = !isExpand
            },
        color = surfaceColor,

        ) {
        Row(modifier = modifier.padding(all = 8.dp)) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = "profile picture",
                modifier = Modifier
                    .size(50.dp)
                    .clip(CircleShape)
                    .border(1.5.dp, MaterialTheme.colorScheme.secondary, shape = CircleShape)
            )
            Spacer(modifier = Modifier.padding(horizontal = 8.dp))
            Column {
                Text(
                    text = msg.author,
                    color = MaterialTheme.colorScheme.secondary,
                    style = MaterialTheme.typography.titleMedium
                )
                Spacer(modifier = Modifier.padding(vertical = 4.dp))
                Text(
                    text = msg.body,
                    style = MaterialTheme.typography.bodyMedium,
                    maxLines = if (isExpand) Int.MAX_VALUE else 1,
                    modifier = Modifier.animateContentSize()
                )

            }
        }
    }


}

//@Preview(name = "Light Mode")
@Preview(showBackground = true, name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun PreviewMessageCard() {
    val messages = listOf(
        Message("Jetpack Compose", "我们更新了"),
        Message("Jetpack Compose 2", "我们更新了,我们更新了,我们更新了,我们更新了"),
        Message("Jetpack Compose 3", "我们更新了,我们更新了,我们更新了,我们更新了"),
        Message("Jetpack Compose 4", "我们更新了,我们更新了,我们更新了,我们更新了"),
        Message("Jetpack Compose 5", "我们更新了,我们更新了,我们更新了,我们更新了"),
        Message("Jetpack Compose 6", "我们更新了,我们更新了,我们更新了,我们更新了")
    )
    Conversation(messages)
//    MessageCard(msg = Message("Jetpack Compose", "我们更新了"), modifier = Modifier.padding(4.dp))
}

@Composable
fun Conversation(messages: List<Message>) {
    LazyColumn {
        items(messages) { message ->
            MessageCard(msg = message, modifier = Modifier.padding(all = 4.dp))
        }
    }
}



